package com.firer.test;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

/**
 * Created by root on 16-2-14.
 */
public class Adapter extends SelectableAdapter<Adapter.ViewHolder> {

    private static final int TYPE_INACTIVE = 0;
    private static final int TYPE_ACTIVE = 1;
    private List<Item> items;
    private static final int ITEM_COUNT = 10  ;
    private ViewHolder.ClickListener clickListener;

    public Adapter(ViewHolder.ClickListener clickListener) {
        super();

        this.clickListener = clickListener;

        // Create some items
        Random random = new Random();
        items = new ArrayList<>();
        for (int i = 0; i < ITEM_COUNT; ++i) {
            items.add(new Item("Item " + i, "This is the item number " + i, random.nextBoolean()));
        }
    }

    public void removeItem(int position) {
        items.remove(position);
        notifyItemRemoved(position);
    }

    public void removeItems(List<Integer> pos) {
        Collections.sort(pos, new Comparator<Integer>() {
            @Override
            public int compare(Integer lhs, Integer rhs) {
                return rhs - lhs;//从大到小排列
            }
        });

        while(!pos.isEmpty()) {
            Log.d("dhy","96");
            if(pos.size() == 1) {
                removeItem(pos.get(0));
                pos.remove(0);
            } else {
                Log.d("dhy","101");
                int cnt = 1;
                //当list的大小比cnt大 并且 pos的第cnt个元素 等于 pos的第cnt-1个元素的值-1
                while(pos.size() > cnt && pos.get(cnt).equals(pos.get(cnt-1)-1) ) {
                    ++cnt;
                }//寻找连续的
                if( cnt==1 ) {
                    removeItem(pos.get(0));
                } else {
                    removeRange(pos.get(cnt-1), cnt);
                }
                for(int i=0; i<cnt; i++) {
                    pos.remove(0);
                }
                Log.d("dhy","115");
            }
        }
    }

    public void removeRange(int posStart, int cnt) {
        for(int i=0; i<cnt; i++) {
            items.remove(posStart);
        }
        //notifyItemRangeChanged(posStart,cnt);
        notifyItemRangeRemoved(posStart, cnt);//TODO 完全是马虎，把Remove写成了Change，找原因找了很长时间
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final int layout = viewType == TYPE_INACTIVE ? R.layout.item : R.layout.item_active;

        View v = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
        return new ViewHolder(v, clickListener);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Item item = items.get(position);
        holder.title.setText(item.getTitle());
        holder.subtitle.setText(item.getSubtitle() + ", which is " + (item.isActive() ? "active" : "inactive"));

        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();

        if(lp instanceof StaggeredGridLayoutManager.LayoutParams) {
            StaggeredGridLayoutManager.LayoutParams sglm = (StaggeredGridLayoutManager.LayoutParams)lp;
            sglm.setFullSpan(item.isActive());
            holder.itemView.setLayoutParams(sglm);
        }

        holder.selectedOverlay.setVisibility(isSelected(position)?View.VISIBLE:View.INVISIBLE);
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    @Override
    public int getItemViewType(int position) {
        final Item item = items.get(position);

        return item.isActive() ? TYPE_ACTIVE : TYPE_INACTIVE;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,
            View.OnLongClickListener {
        @SuppressWarnings("unused")
        private static final String TAG = ViewHolder.class.getSimpleName();

        TextView title;
        TextView subtitle;
        View selectedOverlay;

        private ClickListener listener;

        public ViewHolder(View itemView, ClickListener listener) {
            super(itemView);

            title = (TextView) itemView.findViewById(R.id.title);
            subtitle = (TextView) itemView.findViewById(R.id.subtitle);
            selectedOverlay = itemView.findViewById(R.id.selected_overlay);

            this.listener = listener;

            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.onItemClicked(getPosition());
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (listener != null) {
                return listener.onItemLongClicked(getPosition());
            }

            return false;
        }

        public interface ClickListener {
            void onItemClicked(int position);
            boolean onItemLongClicked(int position);
        }
    }
}

